<?php
class CUtilHelper {

	public static function splitLenth($str,$length = 10){
		mb_internal_encoding ('UTF-8');
		if($length == 0 || mb_strlen($str)<= $length){
			return array($str);
		}
		$strs = array();
		do{
			$strs[] = mb_substr($str,0,$length);
			$str = mb_substr($str,$length);
		}while(mb_strlen($str)>0);
		
		return $strs;
	}
	public static function random($length = 6) {
		$chars = array_merge(range(0, 9), range('a', 'z'), range('A', 'Z'));
		shuffle($chars);
		$password = implode(array_slice($chars, 0, $length));
		return;
	}

	public static function chartLink($data, $x_indictor = array (),$count = 25) {

		if (!is_array($data) || !is_array($x_indictor)) {
			throw Exception('$data,$x_indictor must be array.');
		}

		if (empty ($x_indictor)) {
			$x_indictor = array_keys($data);
		}
		
		$link = 'http://chart.apis.google.com/chart?cht=lc&' .
		'chs=880x125&' .
		'chd=t:{data}&' .
		'chco=67A800&' .
		'chxt=x,y&' .
		'chxl=0:|{x}|1:|{y}&' .
		'chds={min},{max}&' .
		'chm=B,E4F0D8,0,0,0|o,67A800,0,-{scale},10|o,FFFFFF,0,-1,{size}&' . //N,FF0000,-4,,12|
		'chls=3&' .
		'chf=bg,s,FFFFFF|c,s,FFFFFF&' .
		'chg=0,50,0';
		
		$scale = ceil(count($data)/$count);
		if($scale <1) $scale = 1;
		$size = ceil(7/$scale);
		
		$tmp_data = $data;
		asort($tmp_data);
		$min = array_shift($tmp_data);
		$max = array_pop($tmp_data);
		if($max == 0){
			$max++;
		}
		$step = (int)(($max - $min) / 5);
		if ($step < 1)
			$step = 1;
		$min = $min > 0 ? 0 : $min;
		$y_indictor = range($min, $max,  $step);
		if($y_indictor[count($y_indictor)-1] != $max){
			$y_indictor[] = $y_indictor[count($y_indictor)-1]+$step;
		}

		$link = strtr($link, array (
			'{data}' => implode(',', $data),
			'{x}' => implode('|', $x_indictor),
			'{y}' => implode('|', $y_indictor),
			'{min}' => $min,
			'{max}' => $max,
			'{scale}'=>$scale,
			'{size}'=>$size,

			
		));
		return $link;

	}
	
	/**
	 * 生成缩略Url
	 * @param unknown_type $pid
	 * @param unknown_type $width
	 * @param unknown_type $height
	 */
	public static function picUrl($pid,$width=0,$height=0){
		return Yii::app()->createUrl('picture/thumbnail',array('pid'=>$pid,'width'=>$width,'height'=>$height));
		
	}
	
	public static function picUrl_1280x960($pid){
		return self::picUrl($pid,1280,960);
		
	}
	public static function picUrl_640x400($pid){
		return self::picUrl($pid,640,400);
		
	}
	/**
	 * 90mm*55mm
	 * @param unknown_type $pid
	 */
	public static function picUrl_340x210($pid){
		return self::picUrl($pid,340,210);
		
	}
  
  public static function picUrl_340x300($pid){
    return self::picUrl($pid,340,300);
    
  }
	public static function picUrl_340x340($pid){
		return self::picUrl($pid,340,340);
		
	}
	public static function picUrl_400x400($pid){
		return self::picUrl($pid,400,400);
		
	}
}
?>